<?php
namespace app\web\controller;
use think\Controller;
use app\web\model\Attach as AttachModel;
use com\Http as HttpClass;
/*
文件上传类()
{
  "code": 0 //0表示成功，其它失败
  ,"msg": "" //提示信息 //一般上传失败后返回
  ,"data": {
    src: "图片路径"
    ,title: "图片名称" //可选
  }
}
*/
class Upload extends Controller{
	public function _initialize(){		
		// 判断是否登录，没有登录跳转登录页面
		if(!session('user_auth') || !session('user_auth_sign')){
			$this->redirect('index/login');
		}
	}
	public function uploadimage(){
        //var_dump(input('post.'));
        // 获取表单上传文件 例如上传了001.jpg
        $mattach = new AttachModel();
        //$hash = 
        $file = request()->file('file');
        $data['size'] = $file->getSize();
        // 移动到框架应用根目录/public/uploads/ 目录下
        $info = $file->move(ROOT_PATH . 'public' . DS . 'uploads');

        
        if($info){
            // 成功上传后 获取上传信息
            // 输出 jpg
            //echo $info->getExtension();
            // 输出 20160820/42a79759f284b767dfcb2a0197904287.jpg
            //echo $info->getSaveName();
            // 输出 42a79759f284b767dfcb2a0197904287.jpg
            //echo $info->getFilename();
            $ref['code'] = 0; 
            $ref['data']['src'] = '/uploads/'.$info->getSaveName();
            $data['hash'] = $info->md5();
            $data['pathname'] = $info->getSaveName();
            $data['filename'] =  $_FILES['file']['name'];
            $data['is_img'] = 1;

            $mattach->save($data);
        }else{
            // 上传失败获取错误信息
            //echo $file->getError();
            $ref['code'] = -1;
            $ref['msg'] = $file->getError();
        }
        return json($ref);
    }
    //获取文件上传对话框
	public function uppage(){
        return view('uppage');
    }
    //保存附件
	public function upfile(){
        // 获取表单上传文件 例如上传了001.jpg
        $mattach = new AttachModel();
        //$hash = 
        $file = request()->file('upfile');
        $data['size'] =  $file->getSize();
        $size = $file->getSize();
        // 移动到框架应用根目录/public/uploads/ 目录下
        $info = $file->move(ROOT_PATH . 'public' . DS . 'uploads');
        if($info){
            // 成功上传后 获取上传信息
            // 输出 jpg
            //echo $info->getExtension();
            // 输出 20160820/42a79759f284b767dfcb2a0197904287.jpg
            //echo $info->getSaveName();
            // 输出 42a79759f284b767dfcb2a0197904287.jpg
            //echo $info->getFilename();
            $ref['code'] = 0; 
            $pics = '/uploads/'.$info->getSaveName();
            $name=  $_FILES['upfile']['name']; 
            $data['hash'] = $info->md5();
            $data['pathname'] = $info->getSaveName();
            $data['filename'] =  $_FILES['upfile']['name'];

            if(isimage(ROOT_PATH . 'public' . DS . 'uploads/'.$data['pathname'])){
                $data['is_img'] = 1;
            }else{
                $data['is_img'] = 0;
            }

            $mattach->save($data);
            $id= $mattach->id;
        }else{
            // 上传失败获取错误信息
            //echo $file->getError();
            $ref['code'] = -1;
            $ref['msg'] = $file->getError();
        }
        $size = round($size/1024,2);
        $arr = array(
            'name'=>$name,
            'pic'=>$pics,
            'size'=>$size,
            'id' => $id
        );
        echo json_encode($arr);
    }

    // 下载
    public function download()
    {
        $mattach = new AttachModel();
        if($attach = $mattach->where('id',input('id'))->find()) {
            $filename   =   ROOT_PATH . 'public' . DS . 'uploads/' .$attach->pathname;
            //echo $filename;
            if(is_file($filename)) {
                $showname = auto_charset($attach->filename,'utf-8','gbk');
                if(!isset($_SESSION['downloadattach_'.input('id')])) {
                    // 下载计数
                    $mattach->where('id',input('id'))->setInc('count');
                    $_SESSION['downloadattach_'.input('id')]   =  true;
                }
                //添加下载记录

                //需要开启PHP的php_fileinfo扩展
                //$http = new \Org\Net\Http();
                $http = new HttpClass();
                $http->download($filename,$showname);
            }else{
               // $this->error('附件不存在或者已经删除！');
            }
        }else{
            $this->error('附件不存在或者已经删除！');
        }
    }

}